package com.chart.web.controller.chart;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chart.chart.domian.ChartExplainMenu;
import com.chart.chart.domian.ChartUserExplain;
import com.chart.chart.domian.vo.ChartListVo;
import com.chart.chart.domian.vo.ChartMenuVo;
import com.chart.chart.domian.vo.ChartsVo;
import com.chart.chart.service.IChartExplainMenuService;
import com.chart.chart.service.IChartUserExplainService;
import com.chart.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 此处写该类描述
 *
 * @author 罗继全
 * 2024/9/5 13:43
 */
@RestController
@RequestMapping("/chart/explain/menu")
public class ChartExplainMenuController {

    @Autowired
    private IChartExplainMenuService chartExplainMenuService;

    @Autowired
    private IChartUserExplainService chartUserExplainService;


    /**
     * 使用说明
     * @return
     */

    @GetMapping("/list")
    public AjaxResult listAllMenus() {
        // 构建 LambdaQueryWrapper 对象，用于按照 sort 字段升序排序
        LambdaQueryWrapper<ChartExplainMenu> lqw = new LambdaQueryWrapper<>();
        lqw.orderByAsc(ChartExplainMenu::getSort);  // 根据 sort 字段升序排序
        // 获取所有按照 sort 字段排序的 ChartExplainMenu
        List<ChartExplainMenu> menuList = chartExplainMenuService.list(lqw);

        // 遍历每个菜单，查询其对应的子项（ChartUserExplain）
        for (ChartExplainMenu menu : menuList) {
            Long menuId = menu.getId();
            // 使用 LambdaQueryWrapper 来查询 ChartUserExplain 关联的子项
            LambdaQueryWrapper<ChartUserExplain> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ChartUserExplain::getExplainMenu, menuId);  // 根据 explainMenu 字段查询

            // 查询结果并将其作为子项存储到当前菜单的 children 字段
            List<ChartUserExplain> chartUserExplainList = chartUserExplainService.list(queryWrapper);
            menu.setChildren(chartUserExplainList);  // 设置子项
        }

        // 返回带有子项的菜单列表，且菜单已按照 sort 字段升序排序
        return AjaxResult.success(menuList);
    }



}
